﻿
Imports std
Imports storoomc
Imports storcore

Public Class mapslices
    Implements storageInterface
    Private m As map(Of String, bytes) = Nothing
    Private _memoryUsage As UInt64
    Private _valuesize As UInt64

    Public Sub start()
        Dim memUsageStr As String = Nothing
        memUsageStr = config.parameter(configNaming.memslices.name, _
                                       configNaming.memslices.memoryUsage, _
                                       configNaming.memslices.defaultValue.memoryUsage)
        If strsame(memUsageStr, "auto", False) OrElse _
           Not (convertor.touint64(memUsageStr, _memoryUsage)) Then
            _memoryUsage = 0
        End If
        If m Is Nothing Then
            m = New map(Of String, bytes)()
        End If
    End Sub

    Private Function valuesize() As UInt64
        Return _valuesize
    End Function

    Private Function memoryUsage() As UInt64
        If _memoryUsage = 0 Then
            Return availablePhysicalMemory() >> 1
        Else
            Return _memoryUsage
        End If
    End Function

    Public Function capacity(ByRef cap As UInt64) As Boolean Implements storageInterface.capacity
        cap = memoryUsage() - valuesize()
        Return True
    End Function

    Public Function close() As Boolean Implements storageInterface.close
        Return True
    End Function

    Public Function delete(ByVal key As String) As Boolean Implements storageInterface.delete
        Dim it As map(Of String, bytes).iterator = Nothing
        it = m.find(key)
        If it = m.end() Then
            Return False
        Else
            _valuesize -= (+it).second.size()
            m.erase(it)
            Return True
        End If
    End Function

    Public Function empty() As Boolean Implements storageInterface.empty
        Return m.empty()
    End Function

    Public Function full() As Boolean Implements storageInterface.full
        Return valuesize() >= memoryUsage()
    End Function

    Public Function keycount(ByRef count As UInt64) As Boolean Implements storageInterface.keycount
        count = m.size()
        Return True
    End Function

    Public Function health() As Boolean Implements storageInterface.health
        Return Not m Is Nothing
    End Function

    Public Function list(ByRef keys As vector(Of String)) As Boolean Implements storageInterface.list
        If keys Is Nothing Then
            keys = New vector(Of String)()
        Else
            keys.clear()
        End If
        Dim it As map(Of String, bytes).iterator = Nothing
        it = m.begin()
        While it <> m.end()
            keys.push_back((+it).first())
            it += 1
        End While
        Return True
    End Function

    Public Function modify(ByVal key As String, ByVal buff As bytes) As Boolean Implements storageInterface.modify
        Dim it As map(Of String, bytes).iterator = Nothing
        it = m.find(key)
        If it = m.end() Then
            Return writeNew(key, buff)
        Else
            _valuesize += buff.size() - (+it).second.size()
            copy((+it).second, buff)
        End If
        Return True
    End Function

    Public Function open() As Boolean Implements storageInterface.open
        Return True
    End Function

    Public Function read(ByVal key As String, ByRef buff As bytes) As Boolean Implements storageInterface.read
        Dim it As map(Of String, bytes).iterator = Nothing
        it = m.find(key)
        If it = m.end() Then
            Return False
        Else
            copy(buff, (+it).second)
            Return True
        End If
    End Function

    Public Function retire() As Boolean Implements storageInterface.retire
        m.clear()
        _valuesize = 0
        Return True
    End Function

    Public Function seek(ByVal key As String) As Boolean Implements storageInterface.seek
        Return m.find(key) <> m.end()
    End Function

    Public Function sizeof(ByVal key As String, ByRef size As UInt64) As Boolean Implements storageInterface.sizeof
        Dim it As map(Of String, bytes).iterator = Nothing
        it = m.find(key)
        If it = m.end() Then
            Return False
        Else
            size = (+it).second.size()
            Return True
        End If
    End Function

    Public Function status(ByRef allSlicesCount As UInt64, _
                           ByRef unhealthSlicesCount As UInt64) As Boolean Implements storageInterface.status
        allSlicesCount = 1
        unhealthSlicesCount = 0
        Return True
    End Function

    Public Function sync(ByVal desthost As String, ByVal destport As Int32) As Boolean Implements storageInterface.sync
        Return synchelper(Me, desthost, destport)
    End Function

    Private Function writeNew(ByVal key As String, ByVal buff As bytes) As Boolean
        m(key) = buff
        _valuesize += buff.size()
        Return True
    End Function

    Public Function uniqueWrite(ByVal key As String, ByVal buff As bytes) As Boolean _
        Implements storageInterface.uniqueWrite
        If seek(key) Then
            Return False
        Else
            Return writeNew(key, buff)
        End If
    End Function

    Public Function valuesize(ByRef size As UInt64) As Boolean Implements storageInterface.valuesize
        size = valuesize()
        Return True
    End Function

    Public Function write(ByVal key As String, ByVal buff As bytes) As Boolean Implements storageInterface.write
        _valuesize += buff.size()
        m(key) += buff
        Return True
    End Function
End Class
